package com.tsingtao.springboot2.api.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsingtao.springboot2.dto.Document;
import com.tsingtao.springboot2.service.DocumentService;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/document")
public class DocumentController {

    @Autowired
    private DocumentService documentService;

    @PostMapping("/create-index")
    public Boolean createIndex(@RequestBody Document document) {
        return documentService.createIndex(document);
    }


    @PostMapping("/create-index-by-wrapper")
    public Boolean testCreateIndexByWrapper() {
        return documentService.testCreateIndexByWrapper();
    }

    @PostMapping("/test-insert")
    public Document testInsert(@RequestBody Document document) {
        return documentService.testInsert(document);
    }

    @GetMapping("/test-search")
    public List<Document> search() {
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(Document::getTitle, "t1");
        return documentService.selectList(wrapper);
    }

    @PutMapping("/test-update-index")
    public Boolean testUpdateIndex() {
        return documentService.testUpdateIndex();
    }

    @GetMapping("/test-es-page")
    public IPage<Document> testEsPage(EsPageInfo<Document> pageInfo, @RequestBody Document document) {
        EsPageInfo<Document> documentEsPageInfo = documentService.testEsPage(pageInfo, document);
        Page<Document> page = new Page<>(documentEsPageInfo.getPageNum(), documentEsPageInfo.getPageSize());
        page.setRecords(documentEsPageInfo.getList());
        return page;
    }

    @GetMapping("/request-param-type/{organizationId}/{projectId}")
    public Map<String, Object> reqParamType(@PathVariable(value = "organizationId") Long tenantId, @PathVariable Long projectId) {
        Map<String, Object> build = MapUtil.<String, Object>builder().put("tenantId", tenantId).put("projectId", projectId).build();
        return build;
    }

}
